const MapKeys = require('../template')

console.log("=== MapKeys 高级方法测试 ===")

// 测试深度映射键
console.log(
  "深度映射键:", (() => {
    const obj = { a: { b: { c: 1 } }, d: { e: 2 } }
    const mapped = MapKeys.mapKeysDeep(obj, (value, key) => key.toUpperCase(), 2)

    return typeof mapped === 'object' &&
      mapped.A === obj.a &&
      mapped.D === obj.d &&
      !('a' in mapped) &&
      !('d' in mapped)
  })()
)

// 测试条件映射键
console.log(
  "条件映射键:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4 }
    const mapped = MapKeys.mapKeysWithCondition(obj, (value, key) => `new_${key}`, (value, key) => value > 2)

    return typeof mapped === 'object' &&
      mapped.new_c === 3 &&
      mapped.new_d === 4 &&
      !('a' in mapped) &&
      !('b' in mapped) &&
      !('c' in mapped) &&
      !('d' in mapped)
  })()
)

// 测试类型映射键
console.log(
  "类型映射键:", (() => {
    const obj = { a: 1, b: 'test', c: true, d: 2 }
    const mapped = MapKeys.mapKeysWithType(obj, (value, key) => `num_${key}`, 'number')

    return typeof mapped === 'object' &&
      mapped.num_a === 1 &&
      mapped.num_d === 2 &&
      !('a' in mapped) &&
      !('b' in mapped) &&
      !('c' in mapped) &&
      !('d' in mapped)
  })()
)

// 测试正则映射键
console.log(
  "正则映射键:", (() => {
    const obj = { user_name: 'John', user_age: 30, admin_id: 1, admin_role: 'admin' }
    const mapped = MapKeys.mapKeysWithPattern(obj, (value, key) => key.replace(/^user_/, 'person_'), /^user_/)

    return typeof mapped === 'object' &&
      mapped.person_name === 'John' &&
      mapped.person_age === 30 &&
      !('user_name' in mapped) &&
      !('user_age' in mapped) &&
      !('admin_id' in mapped) &&
      !('admin_role' in mapped)
  })()
)

// 测试值映射键
console.log(
  "值映射键:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const mapped = MapKeys.mapKeysWithValue(obj, (value, key) => `key_${value}`, (value, key) => value * 2)

    return typeof mapped === 'object' &&
      mapped.key_2 === 2 &&
      mapped.key_4 === 4 &&
      mapped.key_6 === 6 &&
      !('a' in mapped) &&
      !('b' in mapped) &&
      !('c' in mapped)
  })()
)

// 测试验证映射键
console.log(
  "验证映射键:", (() => {
    const obj = { name: 'John', age: 30, email: 'john@example.com' }
    const schema = {
      name: { type: 'string', required: true },
      age: { type: 'number', min: 0, max: 150 },
      email: { type: 'string', pattern: /@/ }
    }

    try {
      const mapped = MapKeys.mapKeysWithValidation(obj, (value, key) => `valid_${key}`, schema)
      return typeof mapped === 'object' &&
        mapped.valid_name === 'John' &&
        mapped.valid_age === 30 &&
        mapped.valid_email === 'john@example.com' &&
        !('name' in mapped) &&
        !('age' in mapped) &&
        !('email' in mapped)
    } catch (error) {
      return false
    }
  })()
)

// 测试异步映射键
console.log(
  "异步映射键:", (() => {
    const obj = { a: 1, b: 'test' }

    return MapKeys.mapKeysAsync(obj, (value, key) => key.toUpperCase(), async (value, key) => {
      await new Promise(resolve => setTimeout(resolve, 1))
      return value > 0
    }).then(mapped => {
      return typeof mapped === 'object' &&
        mapped.A === 1 &&
        !('a' in mapped) &&
        !('b' in mapped)
    }).catch(() => false)
  })()
)

// 测试并发映射键
console.log(
  "并发映射键:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }

    return MapKeys.mapKeysConcurrent(obj, (value, key) => `new_${key}`, async (value, key) => {
      await new Promise(resolve => setTimeout(resolve, 1))
      return value > 2
    }, 3).then(mapped => {
      return typeof mapped === 'object' &&
        mapped.new_c === 3 &&
        mapped.new_d === 4 &&
        mapped.new_e === 5 &&
        !('a' in mapped) &&
        !('b' in mapped) &&
        !('c' in mapped) &&
        !('d' in mapped) &&
        !('e' in mapped)
    }).catch(() => false)
  })()
)

// 测试统计映射键
console.log(
  "统计映射键:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const result = MapKeys.mapKeysWithStats(obj, (value, key) => key.toUpperCase())

    return typeof result === 'object' &&
      typeof result.data === 'object' &&
      result.data.A === 1 &&
      result.data.B === 2 &&
      result.data.C === 3 &&
      typeof result.stats === 'object' &&
      result.stats.keysMapped > 0
  })()
)

// 测试复杂嵌套结构映射键
console.log(
  "复杂嵌套结构映射键:", (() => {
    const obj = {
      user: {
        profile: {
          personal: {
            name: 'John',
            age: 30,
            address: {
              city: 'New York',
              country: 'USA'
            }
          },
          preferences: {
            theme: 'dark',
            language: 'en'
          }
        },
        posts: [
          { title: 'Post 1', likes: 10 },
          { title: 'Post 2', likes: 20 }
        ]
      },
      meta: { total: 2, page: 1 }
    }

    const mapped = MapKeys.mapKeys(obj, (value, key) => `api_${key}`)

    return typeof mapped === 'object' &&
      mapped.api_user === obj.user &&
      mapped.api_meta === obj.meta &&
      !('user' in mapped) &&
      !('meta' in mapped)
  })()
)

// 测试数组路径映射键
console.log(
  "数组路径映射键:", (() => {
    const obj = { data: { items: [1, 2, 3] } }
    const mapped = MapKeys.mapKeys(obj, (value, key) => `new_${key}`)

    return typeof mapped === 'object' &&
      mapped.new_data === obj.data &&
      !('data' in mapped)
  })()
)

// 测试边界情况映射键
console.log(
  "边界情况映射键:", (() => {
    const obj = { '': 'empty key', ' ': 'space key', 'a': 'normal key' }
    const mapped = MapKeys.mapKeys(obj, (value, key) => key === '' ? 'empty' : `key_${key}`)

    return typeof mapped === 'object' &&
      mapped.empty === 'empty key' &&
      mapped.key_space === 'space key' &&
      mapped.key_a === 'normal key' &&
      !('' in mapped) &&
      !(' ' in mapped) &&
      !('a' in mapped)
  })()
)

// 测试特殊字符映射键
console.log(
  "特殊字符映射键:", (() => {
    const obj = { 'key-with-dash': 'value1', 'key.with.dot': 'value2' }
    const mapped = MapKeys.mapKeys(obj, (value, key) => key.replace(/[-.]/g, '_'))

    return typeof mapped === 'object' &&
      mapped.key_with_dash === 'value1' &&
      mapped.key_with_dot === 'value2' &&
      !('key-with-dash' in mapped) &&
      !('key.with.dot' in mapped)
  })()
)

// 测试数字键映射键
console.log(
  "数字键映射键:", (() => {
    const obj = { 0: 'zero', 1: 'one', 2: 'two' }
    const mapped = MapKeys.mapKeys(obj, (value, key) => `num_${key}`)

    return typeof mapped === 'object' &&
      mapped.num_0 === 'zero' &&
      mapped.num_1 === 'one' &&
      mapped.num_2 === 'two' &&
      !('0' in mapped) &&
      !('1' in mapped) &&
      !('2' in mapped)
  })()
)

// 测试布尔值键映射键
console.log(
  "布尔值键映射键:", (() => {
    const obj = { true: 'yes', false: 'no' }
    const mapped = MapKeys.mapKeys(obj, (value, key) => `is_${key}`)

    return typeof mapped === 'object' &&
      mapped.is_true === 'yes' &&
      mapped.is_false === 'no' &&
      !('true' in mapped) &&
      !('false' in mapped)
  })()
)

// 测试原型链属性映射键
console.log(
  "原型链属性映射键:", (() => {
    function TestClass() {
      this.instanceProp = 'instance'
    }
    TestClass.prototype.prototypeProp = 'prototype'

    const instance = new TestClass()
    const mapped = MapKeys.mapKeys(instance, (value, key) => `new_${key}`)

    return typeof mapped === 'object' &&
      mapped.new_instanceProp === 'instance' &&
      !('instanceProp' in mapped) &&
      !('prototypeProp' in mapped)
  })()
)

// 测试Symbol键映射键
console.log(
  "Symbol键映射键:", (() => {
    const sym = Symbol('test')
    const obj = { [sym]: 'symbol value', normal: 'normal value' }
    const mapped = MapKeys.mapKeys(obj, (value, key) => key === sym ? 'symbol' : `new_${key}`)

    return typeof mapped === 'object' &&
      mapped.symbol === 'symbol value' &&
      mapped.new_normal === 'normal value' &&
      !('normal' in mapped)
  })()
)

// 测试复杂数组结构映射键
console.log(
  "复杂数组结构映射键:", (() => {
    const obj = {
      matrix: [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
      ]
    }
    const mapped = MapKeys.mapKeys(obj, (value, key) => `new_${key}`)

    return typeof mapped === 'object' &&
      mapped.new_matrix === obj.matrix &&
      !('matrix' in mapped)
  })()
)

console.log("=== MapKeys 高级方法测试完成 ===")
